<!--
 * @Author: wangding
 * @Date: 2020-10-28 15:58:26
 * @LastEditTime: 2021-01-26 17:48:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\system\dictionary\add.vue
-->
<template>
    <div>
        <el-dialog
                :title="$t('add')"
            :visible="dictionaryVisible"
            width="600px"
            @close="()=>{$store.commit('setDictionaryVisible',false)}">
            <el-form ref="form" :model="form" label-width="165px" >
                <el-row :gutter="20">
                    <el-col :span="16" :offset="3">
                        <el-form-item :label="$t('dictionary.treeTitle')" prop="dictCode" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.dictCode" :placeholder="$t('common.info.pl-insert-content')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="16" :offset="3">
                        <el-form-item :label="$t('dictionary.code')" prop="code" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.code" :placeholder="$t('common.info.pl-insert-content')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="16" :offset="3">
                        <el-form-item :label="$t('dictionary.value')" prop="value" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.value" :placeholder="$t('common.info.pl-insert-content')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                
                <el-row :gutter="20">
                    <el-col :span="16" :offset="3">
                        <el-form-item :label="$t('dictionary.lang')" prop="lang" :rules="[{ required: true, message: $t('index_js_notNull')}]" >
                            <el-select v-model="form.lang" filterable :placeholder="$t('pleaseSelect')">
                                <el-option
                                v-for="item in langDataList"
                                :key="item.value"
                                :label="item.code"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="16" :offset="3">
                        <el-form-item :label="$t('dictionary.sort')" prop="sort" :rules="[{ required: false, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.sort" :placeholder="$t('common.info.pl-insert-content')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="16" :offset="3">
                        <el-form-item :label="$t('remark')" prop="remark" :rules="[{ required: false, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.remark" :placeholder="$t('common.info.pl-insert-content')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setDictionaryVisible',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {

    },
    props: {
        langDataList:{
            type:Array,
            default:() =>{return []}
        },
        dictCode:{
            type:Object,
            default:() =>{return {}}
        }
    },
    data() {
        return {
            form:{
                dictCode:'',
                code:'',
                value:'',
                lang:'',
                sort:'',
                remark:''
            }
        };
    },
    computed: {
        dictionaryVisible(){
            return this.$store.state.sysStore.dictionaryVisible
        }
    },
    watch: {
        dictCode:{
            handler(nv,ov){
                if(nv){
                    this.form.dictCode = nv.dictCode
                }
            }
        },
        deep:true,
        immediate:true
    },
    created() {

    },
    mounted() {
        this.form.dictCode = this.dictCode.dictCode
    },
    methods: {
        async dictionaryAddEntity(){
            let res = await this.API.dictionaryAddEntity(this.form)
            if(res.success){
                this.$message({
                message: this.$t('index.control_success'),
                type: 'success'
                });
                this.$store.commit('setDictionaryVisible',false)
                this.$emit('uploadData',true)
            }else{
                this.$message({
                message: this.$t('index.control_fail'),
                type: 'warning'
                });
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.dictionaryAddEntity()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" scoped>

</style>
